public class MyQueue {
    static class ListNode {
        private int val;
        private ListNode prev;
        private ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
        private ListNode front;//队头
        private ListNode rear;//队尾
        private int usedSize ;
    //在头插入元素
    public void offer(int x){
        ListNode listNode = new ListNode(x);
        if(front == null){//第一次插入
            front = rear = listNode;
        }else{
            listNode.next = front;
            front.prev = listNode;
            front = listNode;
        }
        usedSize++;
    }
    //出队列  相当于删除尾巴结点
    public int poll(){
        if(front == null){
            throw new NullElementException("没有元素可被删除");
        }
        if(front == rear){
            int x = front.val;
            front = null;
            rear = null;
            usedSize--;
            return x;
        }
        int ret = rear.val;
        rear = rear.prev;
        rear.next = null;
        usedSize--;
        return ret;
    }
    public int peek(){
        if(front == null){
            throw new NullElementException("没有元素可被删除");
        }
        return rear.val;
    }
    public  boolean isEmpty(){
        return usedSize == 0;
    }
    public  int getUsedSize(){
        return usedSize;
    }
}
